#include "RTSPonDemandClient.h"

int main(int argc, char** argv)
{
	RTSPonDemandClient* rtspOnDemandClient;
	rtspOnDemandClient = new RTSPonDemandClient(argv[0]);

	rtspOnDemandClient->initPlaylist();
	rtspOnDemandClient->playAllThePlaylist();

	return 0;
}


RTSPonDemandClient::RTSPonDemandClient(string pgName)
{
	pl=NULL;
	playlistFileName = "playlist.mrp";
	urlToPlay="";
	currentChannel=0;
	progName = pgName;
}


void RTSPonDemandClient::initPlaylist()
{
	string extension;

	extension = playlistFileName.substr((playlistFileName.size()-3), 3);
	if(extension.compare("mrp")!=0)
	{
		cerr << "ERROR : The file name you have provided is not a correct MediaRiver Playlist file " << endl << "Please provide a correct .mrp file !" << endl;
		exit(1);
	}
	else
	{
		pl = new Playlist(playlistFileName);
		pl->openFile();
	}
}


void RTSPonDemandClient::getFirstChannelToPlay()
{
	ch = pl->getChannel(1);
	currentChannel = 1;
	urlToPlay = ch.getUrl();
}


void RTSPonDemandClient::getNextChannelToPlay()
{
	currentChannel++;
	ch = pl->getChannel(currentChannel);
	urlToPlay = ch.getUrl();
}


void RTSPonDemandClient::playAllThePlaylist()
{
	for(unsigned int i=0; i < pl->getNumberOfChannel(); i++)
	{
		if(i==0)
		{
			getFirstChannelToPlay();
		}
		else
		{
			getNextChannelToPlay();
		}

		streamThread = new RTSPStreamThread(progName, urlToPlay, "confFile.mrc");

		streamThread->configureThread();
		streamThread->launchThread();
		streamThread->joinThread();

		delete streamThread;
	}
}










